import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import vue from '@vitejs/plugin-vue'
import { type ConfigEnv, loadEnv, defineConfig } from 'vite'
import UnoCSS from 'unocss/vite'
import { resolve } from 'path'
const pathSrc = resolve(__dirname, 'src')

// Vite配置  https://cn.vitejs.dev/config
export default defineConfig(({ mode }: ConfigEnv) => {
  const env = loadEnv(mode, process.cwd())
  return {
    plugins: [
      vue(),
      UnoCSS(),
      AutoImport({
        // 自动导入vue相关函数
        imports: ['vue', 'vue-router', 'pinia', 'vue-i18n'],
        eslintrc: {
          enabled: true,
          filepath: './.eslintrc-auto-import.json'
        },
        dts: resolve(pathSrc, 'types', 'auto-imports.d.ts')
      }),
      Components({
        // 指定组件位置
        dirs: ['src/components'],
        // 组件的有效文件扩展名
        extensions: ['vue'],
        dts: resolve(pathSrc, 'types', 'components.d.ts'),
        // 自动导入指令
        directoryAsNamespace: true
      })
    ],
    server: {
      host: '0.0.0.0',
      port: +env.VITE_APP_PORT,
      open: true,
      proxy: {
        // 代理 /dev-api 的请求
        [env.VITE_APP_BASE_API]: {
          changeOrigin: true,
          // 代理目标地址：https://api.youlai.tech
          target: env.VITE_APP_API_URL,
          rewrite: path => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
        }
      }
    },
    resolve: {
      alias: {
        '@': pathSrc
      }
    }
  }
})
